﻿using Microsoft.Office.Interop.Excel;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using PanhuaWin.工厂;
using PanhuaWin.系统管理;
using PanhuaWin.通用;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;
using DataTable = System.Data.DataTable;

namespace PanhuaWin.车间.退火
{
    public partial class Form退火报工维护 : UIPage

    {

        string 工厂编号1;
        string 工厂guid1;
        string 部门编号1;
        string FormGuid;
        int 开始行;
        int 结束行;
        string 部门guid;
        string tempguid;
        string 退火原卷guid;
        string FormWorkType = "退火";
        decimal 卷的总重量;
        public Form退火报工维护()
        {
            InitializeComponent();
        }

        private void uiButton选择卷_Click(object sender, EventArgs e)
        {

        }

        private void uiLabel2_Click(object sender, EventArgs e)
        {

        }

        private void Form退火报工维护_Resize(object sender, EventArgs e)
        {
           

        }

        private void Form退火报工维护_Load(object sender, EventArgs e)
        {
            tempguid = Guid.NewGuid().ToString();
            pub.ListWorkShop(pub.sys_user, this.uiComboBoxWorkShop);
            工厂编号1 = uiComboBoxWorkShop.SelectedValue.ToString();
            pub.ListMaterial(工厂编号1, FormWorkType, this.uiComboBox新材质);
            pub.ListWorkLine(this.uiComboBoxWorkShop.SelectedValue.ToString(), "退火", this.uiComboBox产线);
            pub.ListWorkType(this.uiComboBoxWorkShop.SelectedValue.ToString(), "退火", this.uiComboBox去向);
            pub.ListRank(工厂编号1, this.uiComboBox质量等级);
            pub.ListDayShift(工厂编号1, this.uiComboBoxDayShift);
            清理自己mac的Temp_退火报工原卷占用();
            string 当前班组 = pub.Get员工所属班组(pub.sys_user.UserTrueName);
            if (当前班组 == "")
            {
                pub.ListWorkTeam(工厂编号1, this.uiComboBox产线.SelectedValue.ToString(), this.uiComboBoxWorkTeam);
            }
            else
            {
                this.uiComboBoxWorkTeam.Items.Add(当前班组);
                this.uiComboBoxWorkTeam.SelectedIndex = 0;
            }
        }

        private void 清理自己mac的Temp_退火报工原卷占用()
        {
            string sql;
            sql = "delete Temp_退火报工原卷占用 where mac='" + pub.getMac() + "' ";
            pub.ExecuteSql(pub.ConnectionString, sql, "清理自己mac的Temp_退火报工原卷占用,Temp_退火报工原卷占用");
        }

        private void uiLabel31_Click(object sender, EventArgs e)
        {

        }

        private void uiLabel33_Click(object sender, EventArgs e)
        {

        }

        private void uiButton选择已发未完工_Click(object sender, EventArgs e)
        {
            Form已轧硬未退火 form已轧硬未退火 = new Form已轧硬未退火();
            form已轧硬未退火.工厂编号 = this.uiComboBoxWorkShop.SelectedValue.ToString();
            删除自己的tempguid的数据(tempguid);  //Temp_退火报工原卷占用
            if (form已轧硬未退火.ShowDialog(this) == DialogResult.OK)
            {
                this.uiTextBox原料卷号.Text = form已轧硬未退火.卷信息.OutputVolumeNew;
                退火原卷guid = pub.Get退火报工guid(this.uiTextBox原料卷号.Text);
                if (退火原卷guid == "")
                {
                    MessageBox.Show("投入卷号选择有问题，请联系一下系统管理员处理: " + this.uiTextBox原料卷号.Text, "提示", MessageBoxButtons.OK);
                    return;
                }
                this.uiTextBox产出卷号.Text = pub.去掉前缀(this.uiTextBox原料卷号.Text);//不含前缀，不含后面的串号
                                                                            //this.uiTextBox存货编号1.Text = form已酸洗未完工.卷信息.InventoryCode;
                                                                            //this.uiTextBox存货名称1.Text = form已酸洗未完工.卷信息.InventoryName;


                uiTextBox规格型号1.Text = form已轧硬未退火.卷信息.OutputSize;
                string 厚度 = "";
                string 宽度 = "";
                string 虚拟值 = "";
                pub.FromSize(uiTextBox规格型号1.Text, out 厚度, out 宽度, out 虚拟值);
                this.uiTextBox厚度1.Text = 厚度;
                this.uiTextBox宽度1.Text = 宽度;
                this.uiTextBox米数.Text = form已轧硬未退火.卷信息.OutputMeter.ToString();
                this.uiTextBox新厚度.Text = 厚度;
                this.uiTextBox新宽度.Text = 宽度;
                this.uiTextBox产地1.Text = form已轧硬未退火.卷信息.OutputAddress;
                this.uiTextBox卷重量1.Text = form已轧硬未退火.卷信息.OutputWeight.ToString();
                this.uiTextBox新重量.Text = this.uiTextBox卷重量1.Text;
                this.uiTextBox新米数.Text = this.uiTextBox米数.Text;
          this.uiTextBox材质1.Text = form已轧硬未退火.卷信息.OutputMaterial;
                this.uiComboBox新材质.Text = this.uiTextBox材质1.Text;
                this.uiTextBox计量单位1.Text = "Kg";
                // this.uiComboBox去向.SelectedValue = form已酸洗未完工.卷信息.OutputNext;
                ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
              
                塞入自己的tempguid的数据(tempguid, form已轧硬未退火.卷信息.VolumeCodeNew);
            }
            else
            {
                退火原卷guid = "";
                删除自己的tempguid的数据(tempguid);  //Temp_退火报工原卷占用
            }



        }

        private void 塞入自己的tempguid的数据(string tempguid, string volumeCodeNew)
        {
            string sql;
            sql = "insert into Temp_退火报工原卷占用(guid, OriginalVolumeNew, MAC, 时间 ) values('" + tempguid + "','" + volumeCodeNew + "','" + pub.getMac() + "' ,getdate()) ";
            pub.ExecuteSql(pub.ConnectionString, sql, "删除自己的tempguid的数据,Temp_退火报工原卷占用");
        }

        private void 删除自己的tempguid的数据(string tempguid)
        {
            string sql;
            //sql = "delete Temp_退火报工原卷占用 where guid='" + tempguid + "' ";
            sql = "delete Temp_退火报工原卷占用 where mac='" + pub.getMac() + "' ";
            pub.ExecuteSql(pub.ConnectionString, sql, "删除自己的tempguid的数据,Temp_退火报工原卷占用");
        }

        private void uiComboBoxWorkShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            CleallAll();
            工厂编号1 = this.uiComboBoxWorkShop.SelectedValue.ToString();
            pub.ListDayShift(this.uiComboBoxWorkShop.SelectedValue.ToString(), this.uiComboBoxDayShift);
            pub.ListWorkLine(this.uiComboBoxWorkShop.SelectedValue.ToString(), "退火", this.uiComboBox产线);
            pub.ListRank(工厂编号1, this.uiComboBox质量等级);
            pub.ListDayShift(工厂编号1, this.uiComboBoxDayShift);
        }

        private void CleallAll()
        {
            this.uiDataGridView产出.DataSource = null;
            this.uiDataGridView产出.Refresh();
          
            this.uiTextBox产地1.Text = "";
            this.uiCheckBox拆成多卷.Checked = false;
            this.numericUpDown1.Value = 1;
            uiTextBox原料卷号.Text = "";
            this.uiTextBox产出卷号.Text = "";
            退火原卷guid = "";
            删除自己的tempguid的数据(tempguid);

            this.uiTextBox存货编号1.Text = "";
            this.uiTextBox存货名称1.Text = "";
            this.uiTextBox轧制规格.Text = "";
            this.uiTextBox成品规格.Text = "";
            this.uiTextBox备注1.Text = "";
            uiTextBox规格型号1.Text = "";
            string 厚度 = "";
            string 宽度 = "";
            string 虚拟值 = "";

            this.uiTextBox厚度1.Text = 厚度;
            this.uiTextBox宽度1.Text = 宽度;
            this.uiTextBox虚拟值1.Text = 虚拟值;
            this.uiTextBox新厚度.Text = 厚度;
            this.uiTextBox新宽度.Text = 宽度;
            this.uiTextBox产地1.Text = "";
            this.uiTextBox卷重量1.Text = "";
            this.uiTextBox材质1.Text = "";
            //this.uiComboBox新材质.Text = "";
            this.uiTextBox计量单位1.Text = "Kg";
            //this.uiComboBox去向.SelectedValue = form已发未完工卷.出库卷信息.NextWorkType;
            this.uiTextBox计划去向.Text = "";
            uiTextBox质量信息.Text = "";
            uiTextBox新重量.Text = "";
            uiTextBox新规格.Text = "";
            uiTextBox新重量.Text = "";
            uiTextBox轧制规格.Text = "";
         
        }

        private void uiLabel40_Click(object sender, EventArgs e)
        {

        }

        private void uiButton选择部门1_Click(object sender, EventArgs e)
        {

            Form部门档案 form部门档案 = new Form部门档案();
            form部门档案.工厂编号 = this.uiComboBoxWorkShop.SelectedValue.ToString();
            if (form部门档案.ShowDialog(this) == DialogResult.OK)
            {
                this.uiTextBox部门1.Text = form部门档案.部门编号;
                this.uiTextBox部门名称1.Text = form部门档案.部门名称;
                部门guid = form部门档案.Guid.ToString();
                // pub.ListWorkLine(form部门档案.工厂编号, this.uiTextBox部门1.Text, FormWorkType, this.uiTextBox产线搜索1.Text.Trim().Replace("--", ""), this.uiComboBox产线);
            }
        }

        private void uiButton选择辅材_Click(object sender, EventArgs e)
        {
            Form存货档案 form存货档案 = new Form存货档案();
            form存货档案.存货分类 = "生产辅料";
            if (form存货档案.ShowDialog(this) == DialogResult.OK)
            {
              

            }

        }

        private void uiButton辅材批号选择_Click(object sender, EventArgs e)
        {
            string sql;

        }


        private void uiComboBox产线_SelectedIndexChanged(object sender, EventArgs e)
        {
            string workLineCode;
            if (this.uiComboBox产线.SelectedValue.ToString() != "")
            {
                workLineCode = this.uiComboBox产线.SelectedValue.ToString();
                工厂编号1 = this.uiComboBoxWorkShop.SelectedValue.ToString();
                Dic_WorkLine dic_workline = pub.GetWorkLineByLineCode(工厂编号1, workLineCode);
                if (dic_workline.DepartCode != null)
                {
                    Dic_Depart dic_depart = pub.GetDepartByCode(dic_workline.DepartCode, 工厂编号1);
                    if (dic_depart.DepartCode != "")
                    {
                        this.uiTextBox部门1.Text = dic_depart.DepartCode;
                        this.uiTextBox部门名称1.Text = dic_depart.DepartName;
                    }
                    else
                    {
                        this.uiTextBox部门1.Text = "";
                        this.uiTextBox部门名称1.Text = "";
                    }
                }
                else
                {
                    this.uiTextBox部门1.Text = "";
                    this.uiTextBox部门名称1.Text = "";
                }


                pub.ListWorkType(工厂编号1, "退火", this.uiComboBox去向);
                //pub.ListWorkTeam(工厂编号1, workLineCode, this.uiComboBoxWorkTeam);
            }
        }

        private void uiTextBox新厚度_TextChanged(object sender, EventArgs e)
        {
            SetSize();
        }

        private void uiTextBox新宽度_TextChanged(object sender, EventArgs e)
        {
            SetSize();
        }
        private void SetSize()
        {
            this.uiTextBox新规格.Text = pub.Todecimal(this.uiTextBox新厚度.Text, 3) + "*" + pub.ToInt(this.uiTextBox新宽度.Text) + "*C";
        }

        private void uiCheckBox拆成多卷_CheckedChanged(object sender, EventArgs e)
        {
            if (this.uiCheckBox拆成多卷.Checked)
            {
                this.numericUpDown1.Visible = true;
            }
            else
            {
                this.numericUpDown1.Visible = false;
            }
        }

        private void uiButton添加产出卷_Click(object sender, EventArgs e)
        {
            if (pub.Todecimal(this.uiTextBox卷重量1.Text) < 卷的总重量 + pub.Todecimal(this.uiTextBox新重量.Text))
            {
                MessageBox.Show("产出卷的重量超过了投入卷重量,不能单卷报工。", "提示", MessageBoxButtons.OK);
                return;
            }
            if (this.uiCheckBox拆成多卷.Checked == false)
            {
                //单卷
                //已经存在的就不能再次点击添加了
                if (!pub.Is退火报工原始卷被关闭(this.uiTextBox原料卷号.Text))
                {
                    string sql;
                    List<Bus_FireOutput> bus_fireoutputs = pub.Get退火产出卷信息All(this.uiTextBox原料卷号.Text);
                    if (bus_fireoutputs != null)
                    {
                        MessageBox.Show("这个卷已经有产出卷了,不能单卷报工。", "提示", MessageBoxButtons.OK);
                        return;
                    }
                    //if (MessageBox.Show("这卷不再分拆会自动关闭，是否确认?", "询问", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    //{
                        单卷报工();
                    //}
                }
                else
                {
                    MessageBox.Show("这个卷已经被关闭了，不能再次报工了，如需要再次报工，请联系管理员处理。", "提示", MessageBoxButtons.OK);
                    return;
                }

            }
            else
            {
                //单卷
                //已经存在的就不能再次点击添加了
                if (!pub.Is退火报工原始卷被关闭(this.uiTextBox原料卷号.Text))
                {
                    多卷报工();
                }
                else
                {
                    MessageBox.Show("这个卷已经被关闭了，不能再次报工了，如需要强制报工，请联系管理员处理。", "提示", MessageBoxButtons.OK);
                    return;
                }

            }
        }

        private void 多卷报工()
        {
            if (退火原卷guid == "" || this.uiTextBox原料卷号.Text == "")
            {
                MessageBox.Show("没有选择投入卷号", "提示", MessageBoxButtons.OK);
                return;
            }
            string sql;
            //在退火卷分卷时补0，比如01,02
            string 新卷号 = this.uiTextBox前缀.Text + this.uiTextBox产出卷号.Text + this.numericUpDown1.Value.ToString().PadLeft(2, '/');

            Bus_FireOutput bus_fireoutput = new Bus_FireOutput();
            bus_fireoutput.Creator = pub.sys_user.UserName;
            if (this.uiComboBox去向.SelectedText == "")
            {
                MessageBox.Show("去向不能为空", "提示", MessageBoxButtons.OK);
                return;

            }
            if (pub.Get退火产出卷信息(新卷号) != null)
            {
                MessageBox.Show("产出卷号已经存在了，不能添加", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_fireoutput.OutputNext = this.uiComboBox去向.SelectedText;
            bus_fireoutput.WorkLineName = this.uiComboBox产线.SelectedText;
            if (bus_fireoutput.WorkLineName == "")
            {
                MessageBox.Show("产线不能为空", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_fireoutput.WorkLineCode = this.uiComboBox产线.SelectedValue.ToString();
            bus_fireoutput.DayShiftCode = this.uiComboBoxDayShift.SelectedValue.ToString();
            bus_fireoutput.DayShiftName = this.uiComboBoxDayShift.SelectedText;
            
            bus_fireoutput.WorkTeamCode = this.uiComboBoxWorkTeam.SelectedText;
            bus_fireoutput.WorkTeamName = this.uiComboBoxWorkTeam.SelectedText;
            if (bus_fireoutput.WorkTeamName == "")
            {
                MessageBox.Show("班组不能为空", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_fireoutput.DepartCode = this.uiTextBox部门1.Text;
            bus_fireoutput.DepartName = this.uiTextBox部门名称1.Text;
            if (bus_fireoutput.DepartName == "")
            {
                MessageBox.Show("部门不能为空", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_fireoutput.guid = Guid.Parse(退火原卷guid);
            bus_fireoutput.OutputGuid = Guid.NewGuid();
            bus_fireoutput.OutputMaterial = this.uiComboBox新材质.SelectedText;
            bus_fireoutput.OutputQualityGrade = this.uiComboBox质量等级.SelectedText;
            bus_fireoutput.OutputQualityInformation = this.uiTextBox质量信息.Text.Trim().Replace("--", "");
            bus_fireoutput.OutputSize = this.uiTextBox新规格.Text;
            bus_fireoutput.OutputThick = pub.Todecimal(uiTextBox新厚度.Text);
            bus_fireoutput.OutputWidth = pub.Todecimal(uiTextBox新宽度.Text);
            bus_fireoutput.OutputWeight = pub.Todecimal(uiTextBox新重量.Text);
            if (bus_fireoutput.OutputWeight == 0)
            {
                MessageBox.Show("重量不能为0", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_fireoutput.OutputMeter = pub.Todecimal(uiTextBox新米数.Text);
            if (bus_fireoutput.OutputMeter == 0)
            {
                MessageBox.Show("米数不能为0", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_fireoutput.OutputAddress = this.uiTextBox产地1.Text;

            sql = " insert into Bus_FireOutput(OutputGuid, guid, VolumeCodeNew,Row, OutputVolumeNew, OutputSize, OutputThick, OutputWidth, OutputMaterial, OutputWeight, OutputMeter,";
            sql += " OutputQualityGrade, OutputQualityInformation, OutputEndStatus, OutputNext, PrintTimes, DepartCode, DepartName, ";
            sql += " WorkLineCode, WorkLineName, WorkTeamCode, WorkTeamName, DayShiftCode, DayShiftName, CreateID, Creator,";
            sql += " CreateDate,OutputAddress)";
            sql += " values(newid(),'" + 退火原卷guid + "','" + this.uiTextBox原料卷号.Text.Trim().Replace("--", "") + "','" + this.numericUpDown1.Value.ToString() + "','" + 新卷号 + "','" + bus_fireoutput.OutputSize + "','" + bus_fireoutput.OutputThick + "','" + bus_fireoutput.OutputWidth + "',";
            sql += " '" + bus_fireoutput.OutputMaterial + "','" + bus_fireoutput.OutputWeight + "','" + bus_fireoutput.OutputMeter + "','" + bus_fireoutput.OutputQualityGrade + "','" + bus_fireoutput.OutputQualityInformation + "',";
            sql += "  '','" + bus_fireoutput.OutputNext + "',0,'" + bus_fireoutput.DepartCode + "','" + bus_fireoutput.DepartName + "',";
            sql += " '" + bus_fireoutput.WorkLineCode + "','" + bus_fireoutput.WorkLineName + "','" + bus_fireoutput.WorkTeamCode + "','" + bus_fireoutput.WorkTeamName + "',";
            sql += " '" + bus_fireoutput.DayShiftCode + "','" + bus_fireoutput.DayShiftName + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "',";
            sql += " getdate(),'" + bus_fireoutput.OutputAddress + "') ";

            if (pub.ExecuteSql(pub.ConnectionString, sql, "多卷退火报工塞入Bus_FireOriginal") > 0)
            {
                MessageBox.Show("多卷退火报工成功", "提示", MessageBoxButtons.OK);
                //不用自动关闭
                //sql = "update Bus_PicklingOriginal set IsClosed='Y' , ColseUserId='" + pub.sys_user.User_Id + "',ColseUserName='" + pub.sys_user.UserTrueName + "',CloseDate=getdate() where guid='" + 退火原卷guid + "'";
                //pub.ExecuteSql(pub.ConnectionString, sql, "单卷退火报工成功，自动关闭原卷的状态");
                //显示卷信息到表格上
                ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
                return;
            }
        }

        private void 单卷报工()
        {
            if (退火原卷guid == "" || this.uiTextBox原料卷号.Text == "")
            {
                MessageBox.Show("没有选择投入卷号", "提示", MessageBoxButtons.OK);
                return;
            }
            string sql;
            string 新卷号 = this.uiTextBox前缀.Text + this.uiTextBox产出卷号.Text;

            Bus_FireOutput bus_fireoutput = new Bus_FireOutput();
            bus_fireoutput.Creator = pub.sys_user.UserName;
            if (pub.Get退火产出卷信息(新卷号) != null)
            {
                MessageBox.Show("产出卷号已经存在了，不能添加", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_fireoutput.OutputNext = this.uiComboBox去向.SelectedText;
            bus_fireoutput.WorkLineName = this.uiComboBox产线.SelectedText;
            if (bus_fireoutput.WorkLineName == "")
            {
                MessageBox.Show("产线不能为空", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_fireoutput.WorkLineCode = this.uiComboBox产线.SelectedValue.ToString();
            bus_fireoutput.DayShiftCode = this.uiComboBoxDayShift.SelectedValue.ToString();
            bus_fireoutput.DayShiftName = this.uiComboBoxDayShift.SelectedText;

            bus_fireoutput.WorkTeamCode = this.uiComboBoxWorkTeam.SelectedText;
            bus_fireoutput.WorkTeamName = this.uiComboBoxWorkTeam.SelectedText;
            if (bus_fireoutput.WorkTeamName == "")
            {
                MessageBox.Show("班组不能为空", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_fireoutput.DepartCode = this.uiTextBox部门1.Text;
            bus_fireoutput.DepartName = this.uiTextBox部门名称1.Text;
            if (bus_fireoutput.DepartName == "")
            {
                MessageBox.Show("部门不能为空", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_fireoutput.guid = Guid.Parse(退火原卷guid);
            bus_fireoutput.OutputGuid = Guid.NewGuid();
            bus_fireoutput.OutputMaterial = this.uiComboBox新材质.SelectedText;
            bus_fireoutput.OutputQualityGrade = this.uiComboBox质量等级.SelectedText;
            bus_fireoutput.OutputQualityInformation = this.uiTextBox质量信息.Text.Trim().Replace("--", "");
            bus_fireoutput.OutputSize = this.uiTextBox新规格.Text;
            bus_fireoutput.OutputThick = pub.Todecimal(uiTextBox新厚度.Text);
            bus_fireoutput.OutputWidth = pub.Todecimal(uiTextBox新宽度.Text);
            bus_fireoutput.OutputWeight = pub.Todecimal(uiTextBox新重量.Text);
            if (bus_fireoutput.OutputWeight == 0)
            {
                MessageBox.Show("重量不能为0", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_fireoutput.OutputMeter = pub.Todecimal(uiTextBox新米数.Text);
            if (bus_fireoutput.OutputMeter == 0)
            {
                MessageBox.Show("米数不能为0", "提示", MessageBoxButtons.OK);
                return;
            }
            bus_fireoutput.OutputAddress = this.uiTextBox产地1.Text;

            sql = " insert into Bus_FireOriginal(OutputGuid, guid, VolumeCodeNew,Row, OutputVolumeNew, OutputSize, OutputThick, OutputWidth, OutputMaterial, OutputWeight, OutputMeter,";
            sql += " OutputQualityGrade, OutputQualityInformation, OutputEndStatus, OutputNext, PrintTimes, DepartCode, DepartName, ";
            sql += " WorkLineCode, WorkLineName, WorkTeamCode, WorkTeamName, DayShiftCode, DayShiftName, CreateID, Creator,";
            sql += " CreateDate,OutputAddress)";
            sql += " values(newid(),'" + 退火原卷guid + "','" + this.uiTextBox原料卷号.Text.Trim().Replace("--", "") + "','" + this.numericUpDown1.Value.ToString() + "','" + 新卷号 + "','" + bus_fireoutput.OutputSize + "','" + bus_fireoutput.OutputThick + "','" + bus_fireoutput.OutputWidth + "',";
            sql += " '" + bus_fireoutput.OutputMaterial + "','" + bus_fireoutput.OutputWeight + "','" + bus_fireoutput.OutputMeter + "','" + bus_fireoutput.OutputQualityGrade + "','" + bus_fireoutput.OutputQualityInformation + "',";
            sql += "  '','" + bus_fireoutput.OutputNext + "',0,'" + bus_fireoutput.DepartCode + "','" + bus_fireoutput.DepartName + "',";
            sql += " '" + bus_fireoutput.WorkLineCode + "','" + bus_fireoutput.WorkLineName + "','" + bus_fireoutput.WorkTeamCode + "','" + bus_fireoutput.WorkTeamName + "',";
            sql += " '" + bus_fireoutput.DayShiftCode + "','" + bus_fireoutput.DayShiftName + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "',";
            sql += " getdate(),'" + bus_fireoutput.OutputAddress + "') ";
            if (pub.ExecuteSql(pub.ConnectionString, sql, "单卷退火报工塞入Bus_FireOriginal") > 0)
            {
                MessageBox.Show("单卷退火报工成功", "提示", MessageBoxButtons.OK);
                //sql = "update Bus_FireOriginal set IsClosed='Y' , ColseUserId='" + pub.sys_user.User_Id + "',ColseUserName='" + pub.sys_user.UserTrueName + "',CloseDate=getdate() where guid='" + 退火原卷guid + "'";
                //pub.ExecuteSql(pub.ConnectionString, sql, "单卷退火报工成功，自动关闭原卷的状态");
                //显示卷信息到表格上
                ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
                return;
            }


        }

        private void ListOutput(string 投入卷号)
        {
            卷的总重量 = 0;
            string sql;
            sql = "SELECT   OutputGuid, guid, Row, OutputVolumeNew 新卷号, OutputSize 新规格, OutputThick 新厚度, OutputWidth 新宽度, OutputMaterial 新材质, OutputWeight 新重量,OutputMeter 新米数,OutputQualityGrade 质量等级, OutputQualityInformation 质量信息, OutputEndStatus 后续状态, OutputNext 去向,";
            sql += " PrintTimes 打印次数, DepartCode 部门编号, DepartName 生产部门,  WorkLineCode 产线编号, WorkLineName 产线, WorkTeamCode 班组编号, WorkTeamName 班组, DayShiftCode 班别编号, DayShiftName 班别, CreateID, Creator 填报人,  CreateDate 填报时间, ModifyID, Modifier, ModifyDate ";
            sql += " FROM      Bus_FireOutput where   VolumeCodeNew='" + 投入卷号 + "' order by OutputVolumeNew ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb != null)
            {
                uiDataGridView产出.AutoGenerateColumns = false;
                this.uiDataGridView产出.DataSource = tb;
                this.uiDataGridView产出.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                this.uiDataGridView产出.Refresh();
                //产出卷必须连号,不连号不行。
                if (tb.Rows.Count > 0)
                {
                    if (tb.Rows.Count == 1)
                    {
                        卷的总重量 = pub.Todecimal(tb.Rows[0]["新重量"].ToString());
                        //只有一卷
                    }
                    else
                    {
                        for (int i = 0; i < tb.Rows.Count; i++)
                        {
                            卷的总重量 += pub.Todecimal(tb.Rows[i]["新重量"].ToString());
                            if (this.uiTextBox前缀.Text + uiTextBox产出卷号.Text + (i + 1).ToString().PadLeft(2, '/') != tb.Rows[i]["新卷号"].ToString())
                            {
                                MessageBox.Show("产出的卷号不连号请检查", "提示", MessageBoxButtons.OK);
                            }
                        }
                    }

                }
            }


            //显示是否关闭了。
            sql = "select IsClosed from [Bus_FireOriginal] where OriginalVolumeNew='" + 投入卷号 + "' ";
            DataTable tb2 = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb2 != null)
            {
                if (tb2.Rows.Count > 0)
                {
                    if (tb2.Rows[0]["IsClosed"].ToString() == "Y")
                    {
                        this.uiLabelIsClosed.Text = "已关闭";
                    }
                    else
                    {
                        this.uiLabelIsClosed.Text = "未关闭";
                    }
                }
            }

        }

        private void Form退火报工维护_FormClosed(object sender, FormClosedEventArgs e)
        {
            删除自己的tempguid的数据(tempguid);
        }

        private void uiDataGridView产出_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.uiDataGridView产出.Rows.Count > 0)
            {

                if (this.uiDataGridView产出.Columns[e.ColumnIndex].Name == "C删除")
                {
                    if (!pub.Is退火报工原始卷被关闭(this.uiTextBox原料卷号.Text))
                    {
                        string 退火产出卷号 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新卷号"].Value.ToString();
                        string 后续状态 = pub.Get退火产出卷的后续状态(退火产出卷号);
                        if (后续状态 != "未使用")
                        {
                            MessageBox.Show("这个卷状态有问题了", "提示", MessageBoxButtons.OK);
                            //显示卷信息到表格上
                            // this.uiDataGridView产出.Rows[e.RowIndex].Cells["C后续状态"].Value = 后续状态;
                            ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
                            return;
                        }
                        else
                        {   //本人才能删除
                            string 填报人 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C填报人"].Value.ToString();
                            if (填报人 != pub.sys_user.UserTrueName)
                            {
                                MessageBox.Show("您不可以删除其他人员填报的数据", "提示", MessageBoxButtons.OK);
                                return;
                            }
                            if (MessageBox.Show("你要删除这个产出卷么?", "询问", MessageBoxButtons.OKCancel) == DialogResult.OK)
                            {
                                string sql = "delete bus_fireoutput where OutputVolumeNew='" + 退火产出卷号 + "'";
                                pub.ExecuteSql(pub.ConnectionString, sql, "删除产出的退火卷");
                                //原卷的状态还原
                                sql = "update Bus_FireOriginal set IsClosed='N' , ColseUserId=NULL,ColseUserName=NULL,CloseDate=NULL where guid='" + 退火原卷guid + "'";
                                pub.ExecuteSql(pub.ConnectionString, sql, "单卷退火报工取消成功，自动开启原卷的状态");

                                ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
                            }

                        }
                    }
                    else
                    {
                        MessageBox.Show("已经关闭的投入卷不能删除产出卷信息", "提示", MessageBoxButtons.OK);
                        ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
                        return;
                    }
                }
                else if (this.uiDataGridView产出.Columns[e.ColumnIndex].Name == "C打印小标签" && this.uiDataGridView产出.Rows[e.RowIndex].Cells["C去向"].Value.ToString() == "下线入库")
                {

                    Form小标签 form小标签 = new Form小标签();
                    form小标签.生产批号 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新卷号"].Value.ToString();
                    form小标签.规格 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新规格"].Value.ToString();
                    form小标签.钢号 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新材质"].Value.ToString();
                    form小标签.等级 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C质量等级"].Value.ToString();
                    form小标签.净重 = Math.Round(pub.Todecimal(this.uiDataGridView产出.Rows[e.RowIndex].Cells["C新重量"].Value.ToString()), 0).ToString();
                    form小标签.工厂编号 = this.uiComboBoxWorkShop.SelectedValue.ToString();
                    form小标签.来源种类 = "退火小标签";
                    form小标签.存货编号 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C存货编号"].Value.ToString();
                    form小标签.品名 = this.uiDataGridView产出.Rows[e.RowIndex].Cells["C品名"].Value.ToString();
                    form小标签.表面处理 = "";
                    form小标签.表面状态 = "";
                    form小标签.执行标准 = "";
                    form小标签.长度 = "";
                    form小标签.生产日期 = DateTime.Today.ToString("yyyy-MM-dd");
                }


            }
        }

        private void uiButton关闭_Click(object sender, EventArgs e)
        {
            string sql;
            //产出卷必须连号
            sql = "SELECT   OutputGuid, guid, Row, OutputVolumeNew 新卷号, OutputSize 新规格, OutputThick 新厚度, OutputWidth 新宽度, OutputMaterial 新材质, OutputWeight 新重量, OutputQualityGrade 质量等级, OutputQualityInformation 质量信息, OutputEndStatus 后续状态, OutputNext 去向,";
            sql += " PrintTimes 打印次数, DepartCode 部门编号, DepartName 生产部门,  WorkLineCode 产线编号, WorkLineName 产线, WorkTeamCode 班组编号, WorkTeamName 班组, DayShiftCode 班别编号, DayShiftName 班别, CreateID, Creator 填报人,  CreateDate 填报时间, ModifyID, Modifier, ModifyDate ";
            sql += " FROM      Bus_FireOutput where guid='" + 退火原卷guid + "' order by OutputVolumeNew ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb != null)
            {
                uiDataGridView产出.AutoGenerateColumns = false;
                this.uiDataGridView产出.DataSource = tb;
                this.uiDataGridView产出.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                this.uiDataGridView产出.Refresh();
                //产出卷必须连号,不连号不行。
                if (tb.Rows.Count > 0)
                {
                    if (tb.Rows.Count == 1)
                    {
                        //只有一卷
                    }
                    else
                    {
                        for (int i = 0; i < tb.Rows.Count; i++)
                        {
                            if (this.uiTextBox前缀.Text + uiTextBox产出卷号.Text + (i + 1).ToString().PadLeft(2, '/') != tb.Rows[i]["新卷号"].ToString())
                            {
                                MessageBox.Show("产出的卷号不连号请检查", "提示", MessageBoxButtons.OK);
                                return;
                            }
                        }
                    }

                }
            }


            if (MessageBox.Show("你要关闭这个投入卷么?", "询问", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                sql = "update Bus_FireOriginal set IsClosed='Y' , ColseUserId='" + pub.sys_user.User_Id + "',ColseUserName='" + pub.sys_user.UserTrueName + "',CloseDate=getdate() where guid='" + 退火原卷guid + "'";
                pub.ExecuteSql(pub.ConnectionString, sql, "退火手动关闭原卷的状态");
                ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
            }
        }

        private void uiTextBox原料卷号_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (e.KeyChar == 13)
            //{
            //    Bus_HardOriginal bus_hardoriginal = pub.Get退火投入信息(this.uiComboBoxWorkShop.SelectedValue.ToString(), this.uiTextBox原料卷号.Text);
            //        if (bus_hardoriginal!=null)
            //        {
            //        退火原卷guid = bus_hardoriginal.guid.ToString();
            //        this.uiTextBox产出卷号.Text = pub.去掉前缀(this.uiTextBox原料卷号.Text);//不含前缀，不含后面的串号
            //        uiTextBox规格型号1.Text = bus_hardoriginal.OriginalSize;
            //        string 厚度 = "";
            //        string 宽度 = "";
            //        string 虚拟值 = "";
            //        pub.FromSize(uiTextBox规格型号1.Text, out 厚度, out 宽度, out 虚拟值);
            //        this.uiTextBox厚度1.Text = 厚度;
            //        this.uiTextBox宽度1.Text = 宽度;
            //        this.uiTextBox新厚度.Text = 厚度;
            //        this.uiTextBox新宽度.Text = 宽度;
            //        this.uiTextBox卷重量1.Text = bus_hardoriginal.OriginalWeight.ToString();
            //        this.uiTextBox材质1.Text = bus_hardoriginal.OriginalMaterial;
            //        this.uiTextBox米数.Text = bus_hardoriginal.OriginalMeter.ToString();
            //        this.uiComboBox新材质.Text = this.uiTextBox材质1.Text;
            //        this.uiTextBox计量单位1.Text = "Kg";
            //        ListHardOutput(退火原卷guid);
            //        塞入自己的tempguid的数据(tempguid, bus_hardoriginal.OriginalVolumeNew);


            //}








        }
       

      


        private void uiButton搜索已生产卷_Click(object sender, EventArgs e)
        {
            List已生产卷();
        }

        private void List已生产卷()
        {
            Bus_FireOriginal Bus_FireOriginal = pub.Get退火投入信息(this.uiComboBoxWorkShop.SelectedValue.ToString(), this.uiTextBox原料卷号.Text);
            if (Bus_FireOriginal != null)
            {
                退火原卷guid = Bus_FireOriginal.guid.ToString();
                this.uiTextBox产出卷号.Text = pub.去掉前缀(this.uiTextBox原料卷号.Text);//不含前缀，不含后面的串号
                uiTextBox规格型号1.Text = Bus_FireOriginal.OriginalSize;
                string 厚度 = "";
                string 宽度 = "";
                string 虚拟值 = "";
                pub.FromSize(uiTextBox规格型号1.Text, out 厚度, out 宽度, out 虚拟值);
                this.uiTextBox厚度1.Text = 厚度;
                this.uiTextBox宽度1.Text = 宽度;
                this.uiTextBox新厚度.Text = 厚度;
                this.uiTextBox新宽度.Text = 宽度;
                this.uiTextBox卷重量1.Text = Bus_FireOriginal.OriginalWeight.ToString();
                this.uiTextBox材质1.Text = Bus_FireOriginal.OriginalMaterial;
                this.uiTextBox米数.Text = Bus_FireOriginal.OriginalMeter.ToString();
                this.uiComboBox新材质.Text = this.uiTextBox材质1.Text;
                this.uiTextBox产地1.Text = Bus_FireOriginal.OriginalAddress;
                this.uiTextBox计量单位1.Text = "Kg";
                ListOutput(this.uiTextBox原料卷号.Text.Trim().Replace("--", ""));
               
                塞入自己的tempguid的数据(tempguid, Bus_FireOriginal.OriginalVolumeNew);
            }
            else
            {
                MessageBox.Show("没有投入的卷信息，请先选择投入卷 " + this.uiTextBox原料卷号.Text, "提示", MessageBoxButtons.OK);
                return;
            }
        }
       

        private void uiButtonClearAll_Click(object sender, EventArgs e)
        {
            CleallAll();
        }
    }
}
